﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMSModel;
using MainQuoteDBModel;

/// <summary>
/// 市场热点
/// </summary>
public partial class Topics_scrd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        CMSEntities cms = new CMSEntities();
        Topics t = new Topics();
        t.News = new TopicsNews();
        t.News.Content = txtContent.Text;
        t.TypeId = Request["typeid"];
        t.TypeName = cms.TopicsType.First(x => x.ID == t.TypeId).Name;
        t.News.GZD = int.Parse(ddlGZD.SelectedValue);
        t.News.YXL = int.Parse(ddlYXL.SelectedValue);
        t.News.Url = txtUrl.Text;
        var stocks = txtStocks.Text.Split(',', '，');
        t.Stocks = new List<TopicsStock>();
        t.News.Title = txtTitle.Text;
        t.News.ImgUrl = HiddenField1.Value;
        MainQuoteDBEntities stockDAL = new MainQuoteDBEntities();
        foreach (var c in stocks)
        {
            TopicsStock stock = new TopicsStock();
            if (string.IsNullOrEmpty(c))
            {
                continue;
            }
            stock.Stock = c.Trim(); 
            stock.StockName = CommonBll.DicStocks[c];
            stock.HYBH = stockDAL.TB_BK_GG.Where(x => x.GG_CODE == stock.Stock && x.BK_CODE.StartsWith("002")).First().BK_CODE;
            var hymcs = stockDAL.TB_BK.Where(x => x.BK_CODE == stock.HYBH);
            //hymcs.Where(x=>x.BK_CODE.in)
            t.Stocks.Add(stock);
        }
        CMSModel.Topics topics = new CMSModel.Topics();
        topics.Content = Newtonsoft.Json.JsonConvert.SerializeObject(t);
        topics.CTime = DateTime.Now;
        topics.ID = Guid.NewGuid();

        topics.TypeId = Request["typeid"];
        cms.Topics.AddObject(topics);
        cms.SaveChanges();
        ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加成功')；document.location='Default.aspx'", true);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string path = Server.MapPath("~/file/scjd/");
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            string houzui = "";
            if (FileUpload1.FileName.IndexOf(".") > -1)
            {
                houzui = FileUpload1.FileName.Substring(FileUpload1.FileName.IndexOf("."));
            }
            string file = Guid.NewGuid().ToString() + houzui;
            string filePath = path + file;
            FileUpload1.SaveAs(filePath);
            HiddenField1.Value = file;
            Image1.ImageUrl = "~/file/scjd/" + file;
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('上传成功！')", true);
        }
    }
}